//
//  OPEatMGuideController.m
//  OnePiece
//
//  Created by wsy on 15/10/29.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPEatMGuideController.h"
#import "OPRequesterManager.h"
#import "OPMGuideCountryListCell.h"
#import "OPMGuideCountryListHeadView.h"
#import "OPMrestListController.h"
@interface OPEatMGuideController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
@end

@implementation OPEatMGuideController
- (NSMutableArray *)dataSourceArr{
    if (_dataSourceArr == nil) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}

- (void)configureData{
    NSString *urlstr = @"http://www.youyuwei.com/api/label?app_code=com.youyuwei.yuwei.ios&hardware_model=%2C2&app_ver=2.1&sys_ver=&type=1&device_type=ios&ywsdk_ver=20131018&open_udid=&start=0&oauth_timestamp=1446083002&device_token=&x_auth_mode=client_auth&oauth_signature_method=HMAC-SHA1&ver=2&idfa=&channel_id=App%20Store&oauth_version=1.0&oauth_consumer_key=4&oauth_token=&oauth_signature=ljcNNng%2Fsekhu1eZtfjs1bIXA50%3D&debug=0&oauth_nonce=";
    [OPRequesterManager requestWithType:GET urlString:urlstr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *array = dic[@"data"][@"list"][0][@"content"];
        for (NSDictionary *dict in array) {
            OPMguideListModel *model = [[OPMguideListModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            NSLog(@"%@",model.name);
            [self.dataSourceArr addObject:model];
        }
        [_mainCollectionView reloadData];
    } error:^(NSError *error) {
        NSLog(@"米其林指南请求错误%@", [error localizedDescription]);
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftButton setImage:[UIImage imageNamed:@"meishi-back"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    self.titleLable.text = @"米其林全球指南";
    // Do any additional setup after loading the view.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH/2-30, SCREEN_WIDTH/2-30);
    layout.minimumInteritemSpacing = 20;
    layout.minimumLineSpacing = 20;
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH*235/520+50);
    layout.footerReferenceSize = CGSizeZero;
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    _mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    _mainCollectionView.dataSource = self;
    _mainCollectionView.delegate = self;
    _mainCollectionView.showsVerticalScrollIndicator = NO;
    _mainCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mainCollectionView];
    [_mainCollectionView registerClass:[OPMGuideCountryListCell class] forCellWithReuseIdentifier:@"cell"];
    [_mainCollectionView registerClass:[OPMGuideCountryListHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [_mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    [self configureData];
    
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OPMGuideCountryListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    OPMguideListModel *model = _dataSourceArr[indexPath.item];
    [cell setModel:model];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        OPMGuideCountryListHeadView *reusebleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        return reusebleView;
    }else{
        UICollectionReusableView *reusebleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot" forIndexPath:indexPath];
        
        return reusebleView;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    OPMguideListModel *m = _dataSourceArr[indexPath.item];
    OPMrestListController *mrVC = [[OPMrestListController alloc]init];
    mrVC.countryid = m.countryid;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mrVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
